<template>
  <scroll-view class="date-picker" scroll-x show-scrollbar="false">
    <view class="date-list">
      <view
        class="date-item"
        v-for="(item, index) in dates"
        :key="index"
        :class="{ active: item.timestamp === activeDate }"
        @tap="$emit('date-change', item.timestamp)"
      >
        <text class="week">{{ item.weekDay }}</text>
        <text class="date">{{ item.date }}</text>
      </view>
    </view>
  </scroll-view>
</template>
  
<script setup>
import dayjs from "dayjs";
import { ref, onMounted, defineEmits } from "vue";

const props = defineProps({
  activeDate: {
    type: Date,
    required: true,
  },
});
defineEmits(["date-change"]);

// 课程日期列表
const dates = ref([]); 

// 构造最近6天数据
const _getDates = () => {
  const days = [];
  const weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  for (let i = 0; i < 6; i++) {
    const currentDate = dayjs().add(i, "day");
    let weekDay;
    if (i === 0) {
      weekDay = "今天";
    } else if (i === 1) {
      weekDay = "明天";
    } else if (i === 2) {
      weekDay = "后天";
    } else {
      weekDay = weekDays[currentDate.day()];
    }
    days.push({
      weekDay,
      date: currentDate.format("D"),
      timestamp: currentDate.format("YYYY-MM-DD"),
    });
  }
  dates.value = days;
};

onMounted(() => {
  _getDates();
  console.log("date.value:", dates.value);
});
</script>
  
  <style lang="scss">
.date-picker {
  padding: 20rpx 0;
  white-space: nowrap;

  .date-list {
    display: inline-flex;
    gap: 50rpx;

    .date-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 8rpx;

      .week {
        font-size: 30rpx;
        color: #666;
      }

      .date {
        font-size: 32rpx;
        color: #83837f;
      }

      &.active {
        .week,
        .date {
          color: #171717;
          font-weight: bold;
        }
      }
    }
  }
}
</style>